package webdev.fragment;

import java.util.ArrayList;

import webdev.adapters.ListLocationAdapter;
import webdev.common.Common;
import webdev.utils.ConnectionDetectorUtil;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import dev.danang.R;

public class LandingPageFragment extends Fragment {

    private View footer;
    private ListLocationAdapter mLandingPageAdapter;
    private ListView mListViewLandingPage;
    private int mPageCount = 1;
    private boolean mIsFinish = false;
    private boolean mIsNodeFinish = false;
    private boolean mIsArticleFinish = false;
    private LoadDataAPI mLoadDataAPI;
    private ArrayList<String> keySearchs;
    private int indexKeySearch;
    
    // Progress Bar
    private ProgressBar mProgressBar;
    private ProgressBar mProgressBarLoadMore;

    public Fragment newInstance(ArrayList<String> categories, ArrayList<String> keySearchs, int index) {
    	LandingPageFragment landingPageFragment = new LandingPageFragment();
    	Bundle args = new Bundle();
        args.putStringArrayList("LIST_CATEGORY", categories);
        args.putStringArrayList("LIST_KEY_SEARCH", keySearchs);
        args.putInt("INDEX_KEY_SEARCH", index);
        landingPageFragment.setArguments(args);
    	return landingPageFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View layout = inflater.inflate(
                R.layout.youlook_landing_page_fragment,
                null);
        init(layout);
        setEventForMembers();

        // Check the Internet connection is available
        if (ConnectionDetectorUtil.isConnectingToInternet(getActivity())) {
            mLoadDataAPI = new LoadDataAPI();
            mLoadDataAPI.execute();
        } else {
            Toast.makeText(getActivity(),
                    getResources().getString(R.string.message_no_network),
                    Toast.LENGTH_SHORT).show();
        }

        return layout;
    }

    private void init(View layout) {
        mLandingPageAdapter = new ListLocationAdapter(getActivity());
        //code to set adapter to populate list
        footer =  ((LayoutInflater)getActivity().getSystemService(
        		Context.LAYOUT_INFLATER_SERVICE)).inflate(
        				R.layout.footer_list_view_landing_page, null, false);
        mListViewLandingPage = (ListView) layout.findViewById(R.id.list_view_landing_page);
        mListViewLandingPage.addFooterView(footer);
        mListViewLandingPage.setAdapter(mLandingPageAdapter);

        this.keySearchs = getArguments().getStringArrayList("LIST_KEY_SEARCH");
        this.indexKeySearch = getArguments().getInt("INDEX_KEY_SEARCH");
        
        // Progress Bar
        mProgressBar = (ProgressBar) layout.findViewById(R.id.progress_bar_landing_page);
        mProgressBarLoadMore = (ProgressBar) footer.findViewById(R.id.progress_bar_landing_page_load_more);
        mProgressBarLoadMore.setVisibility(View.GONE);
    }

    private void setEventForMembers() {
        mListViewLandingPage.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
//                 Pause fetcher to ensure smoother scrolling when flinging
            	if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
            		Common.IMAGE_LOADER_YOULOOK.setPauseWork(true);
            		Common.IMAGE_LOADER_CIRCLE_YOULOOK.setPauseWork(true);
            	} else {
            		Common.IMAGE_LOADER_YOULOOK.setPauseWork(false);
            		Common.IMAGE_LOADER_CIRCLE_YOULOOK.setPauseWork(false);
            	}
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                final int lastItem = firstVisibleItem + visibleItemCount;
                if (lastItem == totalItemCount) {}
            }
        });
    }

    private class LoadDataAPI extends AsyncTask<String, Void, Void> {

        private LoadDataAPI() {
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(String... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
        }
        
    }

    @Override
    public void onPause() {
    	// TODO Auto-generated method stub
    	super.onPause();
    	Log.i("LandingPage Fragment","On Pause");
    }
    @Override
    public void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	Log.i("LandingPage Fragment","On Resume");
    }
}
